﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcShopping.Models;
using System.Drawing;
namespace MvcShopping.Controllers
{
    public class HomeController : Controller
    {
        MvcShoppingContext1 db = new MvcShoppingContext1();
        //

        public ActionResult Index()
        {

            var data = db.ProdcutCategories.ToList();
            //插入演示数据
            if (data.Count == 0)
            {
                db.ProdcutCategories.Add(
                     new ProductCategory() { Id = 1, Name = "手机" }
                );
                db.ProdcutCategories.Add(
                    new ProductCategory() { Id = 2, Name = "平板" }
                    );
                db.ProdcutCategories.Add(
                    new ProductCategory() { Id = 3, Name = "笔记本" }
                    );
                db.ProdcutCategories.Add(
                    new ProductCategory() { Id = 4, Name = "阅读器" }
                    );
                db.SaveChanges();
                data = db.ProdcutCategories.ToList();
            }

            var member = (from p in db.Products
                          orderby p.SellNums descending
                          select p).ToList();
            ViewData["member"] = member;
            ViewData["data"] = data;
            //ViewData.Model=member;
            return View(Tuple.Create(data, member));
            //return View();
        }

        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ProductList(int id)
        {
            var productCategory = db.ProdcutCategories.Find(id);//所查找的类别
            if (productCategory != null)
            {
                var data = productCategory.Products.ToList();
                if (data.Count == 0)
                {
                    if (id == 1)
                    {
                        productCategory.Products.Add(
                                              new Product()
                                              {
                                                  productCategory = productCategory,
                                                  Name = "iphonX",
                                                  Description = "整部iPhoneX,看起来就是一块几乎整面的屏幕。全新5.8英寸超视网膜显示屏",
                                                  Color = "深空灰色",
                                                  Price = 8388,
                                                  PublishOn = DateTime.Now,
                                                  Url = "images/shouji/IphonX.jpg",
                                                  SellNums = 0
                                              });
                        productCategory.Products.Add(
                           new Product()
                           {
                               productCategory = productCategory,
                               Name = "魅族PRO6",
                               Description = "越强大，越冷静",
                               Color = "深空灰",
                               Price = 2099,
                               PublishOn = DateTime.Now,
                               Url = "images/shouji/魅族.jpg",
                               SellNums = 0
                           });
                    }
                    else if (id == 2)
                    {//礼品
                        productCategory.Products.Add(
                            new Product()
                            {
                                productCategory = productCategory,
                                Name = "iPadPro",
                                Description = "10.5英寸，256G",
                                Color = "玫瑰金色",
                                Price = 5048,
                                PublishOn = DateTime.Now,
                                Url = "images/pinban/iPad.jpg",
                               
                                SellNums = 0
                            });
                        productCategory.Products.Add(
                           new Product()
                           {
                               productCategory = productCategory,
                               Name = "华为M3",
                               Description = "青春版10英寸",
                               Color = "皎月白",
                               Price = 2499,
                               PublishOn = DateTime.Now,
                               Url = "images/pinban/华为M3.jpg",
                               SellNums = 0
                           });
                    }
                    else if (id == 3)
                    {
                        productCategory.Products.Add(
                            new Product()
                            {
                                productCategory = productCategory,
                                Name = "联想ideaPad",
                                Description = "15.6英寸，简约设计",
                                Color = "银灰色",
                                Price = 3199,
                                PublishOn = DateTime.Now,
                                Url = "images/bijiben/lennove.jpg",
                                SellNums = 0
                            });
                        productCategory.Products.Add(
                           new Product()
                           {
                               productCategory = productCategory,
                               Name = "HP pavilion",
                               Description = "15.6英寸轻薄便携i7",
                               Color = "银色",
                               Price = 6599,
                               PublishOn = DateTime.Now,
                               Url = "images/bijiben/hp.jpg",
                               SellNums = 0
                           });
                    }
                    else
                    {
                        productCategory.Products.Add(
                           new Product()
                           {
                               productCategory = productCategory,
                               Name = "Kindle Oasis",
                               Description = "内存增至32GB，可存储数千本图书",
                               Color = "银灰色",
                               Price = 3168,
                               PublishOn = DateTime.Now,
                               Url = "images/kindle/kindle.jpg",
                               SellNums = 0
                           });
                        productCategory.Products.Add(
                           new Product()
                           {
                               productCategory = productCategory,
                               Name = "Kindle Paperwhite经典版",
                               Description = "梵高套装怒放系列",
                               Color = "黑色",
                               Price = 1179,
                               PublishOn = DateTime.Now,
                               Url = "images/kindle/kindle2.jpg",
                               SellNums = 0
                           });
                    }


                    db.SaveChanges();

                }
                data = productCategory.Products.ToList();
                return View(data);
            }
            else
            {
                return HttpNotFound();
            }

        }

        public ActionResult ProductSearch()
        {
            string str = Request.Form["searchcontent"];
            var member = (from p in db.Products
                          where p.Name.Contains(str)
                          select p).ToList();
            return View(member);
        }


        /// <summary>
        /// 商品明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ProductDetail(int id)
        {
            var data = db.Products.Find(id);

            return View(data);
        }


    }
}
